/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2021-02-25 16:19:07
 * @LastEditors: ding-cx
 * @LastEditTime: 2021-02-25 17:26:35
 * @Description: file content
 */

import { IHelper } from "egg";

export default {
  util_create_sequelize_error(this: IHelper, error) {
    /**
     *工具，从原始的sequelize查询错误创建一个错误信息对象返回
     *
     * @memberof BaseController
     */
    this.ctx.coreLogger.error(error); // 记录egg日志
    if (!error) {
      return error;
    }
    const errorObj = {
      name: error.name || "",
      msg: "",
      description: {},
    };

    if (error.errors) {
      errorObj.msg = error.errors.map((item) => item.message);
    }

    if (error.parent) {
      errorObj.msg = error.parent.sqlMessage;
      errorObj.description = {
        code: error.parent.code,
        message: error.parent.sqlMessage || error.parent.message,
        sql: error.parent.sql,
      };
    }

    return errorObj;
  },
};
